//空白区域。
var spaces = [];
/**
 * space :{
	 direction: "L" 表示空白区域在点线的左侧。
	 points:[]
 }
 */

function init(rect = {
	x: 0,
	y: 0,
	width: 464,
	height: 320
}) {
	var points = makeRectPoints(rect);
	var space = {
		points: points
	};
	spaces.push(space);
}


function makeRectPoints(rect = {
	x: 0,
	y: 0,
	width: 464,
	height: 320
}) {
	var points = [];
	points.push({
		x: rect.x,
		y: rect.y,
		d: "D"
	});
	points.push({
		x: rect.x,
		y: rect.y + rect.height,
		d: "R"
	});
	points.push({
		x: rect.x + rect.width,
		y: rect.y + rect.height,
		d: "U"
	});
	points.push({
		x: rect.x + rect.width,
		y: rect.y,
		d: "L"
	});
	return points;
}

//在当前的空白区域下放置一个rect，space可能会被分割成新的或更过的空间。
function addRect(rect) {
	var points2 = makeRectPoints(rect);
	for (var i = 0; i < spaces.length; i++) {
		var space = spaces[i];
		merge(space.points, points)
	}
}

//points1组成的空白区域被points2组成的形状（非空白矩形）合并。
//dir1标记的是points1所围起来的空白方向。
function merge(points1, dir1 = 0, rect) {
	for (var i = 1; i < points1.length; i++) {
		var start = points1[i - 1];
		var to = points1[i];
		var ltor = start.y == to.y && start.x < to.x;
		var rtol = start.y == to.y && start.x > to.x;
		var ttob = start.x == to.x && start.y < to.y;
		var btot = start.x == to.x && start.y > to.y;
		if (ltor) {
			//水平线条的上部
			if (rect.y + rect.height == start.y && rect.x > start.x && rect.x <= to.x) {
				var point = {
					x: rect.x,
					y: start.y
				};
				/////////
			}
		} else if (rtol && rect.y == start.y) {} else if (ttob && rect.x == start.x) {} else if (btot && rect.x + rect
			.width == start.x) {}

	}
}

function transLines(points) {
	var lines = [];
	for (var i = 1; i < points.length; i++) {
		var start = points[i - 1];
		var to = points[i];
		var type = (start.x == to.x) ? "V" : "H";
		var line = {
			start: points[i - 1],
			to: points[i],
			type: k
		};
		lines.push(line);
	}
	return lines;
}




export {
	init,
	addRect
}